package com.ql.controller.patient;

import com.ql.pojo.menzhen.Guahaodan;
import com.ql.pojo.menzhen.User;
import com.ql.service.keshi.KeShiService;
import com.ql.service.menzen.FomularyService;
import com.ql.service.menzen.UserLoginService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 患者登录 下单
 */
@Controller
@RequestMapping("/patient/")
public class Patient {

    @Resource
    public UserLoginService userLoginService;

    @Resource
    public FomularyService fomularyService;


    @RequestMapping(value ={ "zuche"}, method = { RequestMethod.POST})
    //@ResponseBody
    public String zuche( @ModelAttribute("user") User user){
        boolean bl = (boolean) userLoginService.saveUser(user);

        if(bl){
//            return "";
            return "forward:../qiantai/html/zuche/zuche.jsp";
        }else {
            return "forward:../qiantai/html/zuche/zuche.jsp";
        }

    }


    @RequestMapping("doLogin")
    public String doLogin(@RequestParam("userCode") String userCode,
                          @RequestParam("userPassword")String userPassword,
                          HttpSession session, Model model){
        //调用查询服务
        if(userLoginService.doLogin(userCode,userPassword)){
            //把用户存入session:硬编码
            session.setAttribute("userSession",userLoginService.getUserByCode(userCode));
            //跳转挂号页面

//            return "forward:../qiantai/html/zuche/index.jsp";
            return "forward:../qiantai/html/zuche/keshi.jsp";
        }else{
            model.addAttribute("retCode","F");
            model.addAttribute("retMsg","用户名或密码失败");

            return "forward:../qiantai/html/zuche/zuche.jsp";
        }
    }

    @RequestMapping("zhifu")
    public String zhifu(HttpSession httpSession){
        Guahaodan guahaodan = new Guahaodan();
        guahaodan.setGuahaodanId((String)httpSession.getAttribute("guahaodanId"));
        Integer userId = Integer.parseInt(httpSession.getAttribute("userId").toString());
        Integer doctorId = Integer.parseInt(httpSession.getAttribute("doctorId").toString());
        Double money = Double.parseDouble(httpSession.getAttribute("money").toString());
        Integer Kno = Integer.parseInt(httpSession.getAttribute("Kno").toString());
        guahaodan.setUserId(userId);
        guahaodan.setDoctorId(doctorId);
        guahaodan.setMoney(money);
        guahaodan.setKno(Kno);
        fomularyService.addGuahaodan(guahaodan);
        System.out.println(guahaodan.toString());
        return "forward:../qiantai/html/zuche/keshi5.jsp";
    }
    @RequestMapping("sy")
    public String sy(HttpServletRequest request){

        return "redirect:../qiantai/html/index.htm";
    }



}
